clear
set more off

use "Clustering AZ.dta"
g ratio = spending/threshold

****************** Figure 1 ***********************
hist ratio if ratio < 2, by(year) start(0) width(.1) frequency

g bin = floor(ratio*20)
replace bin = (bin/20) + .025 - 1
tab bin year
drop if abs(bin) > .8
g year_bin = year + bin
egen freq = count(spending), by(year_bin)
sort year_bin
drop if year_bin == year_bin[_n-1]
keep year bin freq
reshape wide freq, i(bin) j(year)
preserve
clear
set obs 32
g bin = -.775 + .05*(_n-1)
sort bin
save "bins.dta", replace
restore
sort bin
merge bin using "bins.dta"
drop _merge
forvalues i = 2006(2)2010{
replace freq`i' = 0 if freq`i' == .
}
g threshold = bin > 0
g right = bin * threshold
g left = bin * (1 - threshold)
g freq2006_2008 = freq2006 + freq2008

***************** Rows 1 and 2 of Appendix Table 1 ****************************
reg freq2006_2008 threshold right left, robust
reg freq2010 threshold right left, robust

clear
use "Clustering ME.dta"
g ratio = spending / threshold

****************** Figure 2 ***********************
hist ratio if ratio < 2, start(0) width(.1) by(year) frequency

g bin = floor(ratio*20)
replace bin = (bin/20) + .025 - 1
drop if abs(bin) > .65
egen freq = count(spending), by(bin)
sort bin
drop if bin == bin[_n-1]
keep bin freq
sort bin
merge bin using "bins.dta"
drop _merge
drop if abs(bin) > .65
replace freq = 0 if freq == .
g threshold = bin > 0
g right = bin * threshold
g left = bin * (1 - threshold)

***************** Row 3 of Appendix Table 1 ****************************
reg freq threshold right left, robust

clear
use "Campaign Panel.dta"
g injunction = state == "AZ" & year == 2010
g cand_pop = candidate/pop10
g ind_pop = independent/pop10
g log_cand = log(candidate)
g log_ind = log(independent)
g stateid = 1 if state == "AZ"
replace stateid = 2 if state == "AK"
replace stateid = 3 if state == "CA"
replace stateid = 4 if state == "CO"
replace stateid = 5 if state == "CT"
replace stateid = 6 if state == "FL"
replace stateid = 7 if state == "ME"
replace stateid = 8 if state == "MI"
replace stateid = 9 if state == "OR"
replace stateid = 10 if state == "TN"
g private = candidate
replace private = 14163935 if stateid == 1 & year == 2010
replace private = 5673113 if stateid == 1 & year == 2008
replace private = 5718939 if stateid == 1 & year == 2006
replace private = 3194479 if stateid == 1 & year == 2004
replace private = 9192676 if stateid == 1 & year == 2002
replace private = 10358755 if stateid == 7 & year == 2010
replace private = 489370 if stateid == 7 & year == 2008
replace private = 2593246 if stateid == 7 & year == 2006
replace private = 1189182 if stateid == 7 & year == 2004
replace private = 5756363 if stateid == 7 & year == 2002
replace private = . if stateid == 5
g private_pop = private/pop10
g log_private = log(private)
g az_private = .
replace az_private = private_pop if state == "AZ"
g me_private = .
replace me_private = private_pop if state == "ME"
g statecode = 1 if state == "AZ"
replace statecode = 2 if state == "CA"
replace statecode = 3 if state == "ME"
replace statecode = 4 if state == "MI"
replace statecode = 5 if state == "TN"
replace statecode = 6 if state == "AK"
replace statecode = 7 if state == "CO"
replace statecode = 8 if state == "FL"
replace statecode = 9 if state == "CT"
replace statecode = 10 if state == "OR"

************** Figure 3 **********************
sort year
graph twoway line me_private az_private year if year > 2000

************* Figure 4 ***********************
preserve
drop if year < 2006
drop if statecode > 5
tsset statecode year
synth cand_pop cand_pop pop_growth, tru(1) trp(2010) fig
restore

********* Appendix Table 2 ********************
g available_data = 1
replace available_data = 2 if independent != .
table state year, content(mean available_data)

********* Appendix Table 3 *********************
xi: reg cand_pop injunction i.state i.year, cluster(state)
xi: reg log_cand injunction i.state i.year, cluster(state)
xi: reg private_pop injunction i.state i.year, cluster(state)
xi: reg log_private injunction i.state i.year, cluster(state)
xi: reg ind_pop injunction i.state i.year, cluster(state)
xi: reg log_ind injunction i.state i.year, cluster(state)



